package com.ebt.m.proposal_v2.mvp.presenter;

import android.app.Activity;
import android.util.Log;

import com.ebt.m.commons.buscomponent.listview.BaseIView;
import com.ebt.m.proposal_v2.bean.InsuranceEntity;
import com.ebt.m.proposal_v2.bean.event.MainChooseEvent;
import com.ebt.m.proposal_v2.bean.temp.ActivityFinishStack;
import com.ebt.m.proposal_v2.dao.ProposalApi;
import com.ebt.m.proposal_v2.dao.request.ParamGetRisks;
import com.ebt.m.proposal_v2.dao.response.ResponseMain;
import com.ebt.m.proposal_v2.dao.response.ResponseMainDetail;
import com.ebt.m.proposal_v2.dao.response.TouDanInfo;
import com.ebt.m.proposal_v2.helper.PickerHelper;
import com.ebt.m.proposal_v2.retrofit.OnResponseListener;
import com.ebt.m.proposal_v2.retrofit.ResponseError;
import com.ebt.m.proposal_v2.retrofit.ResponsePage;
import com.ebt.m.proposal_v2.utils.DataUtils;
import com.ebt.m.proposal_v2.widget.dialog.EBTProgressDialog;
import com.ebt.m.utils.JsonUtil;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

/**
 * MainProductAdapter->Presenter 处理点击主险选中实现
 *
 * @author archer.qi
 *         Created on 2017/5/26.
 */
public class MainItemPresenter {
    private Activity mActivity;
    private EBTProgressDialog mProgressDialog;
    private TouDanInfo touDanInfo;

    public MainItemPresenter(Activity activity) {
        mActivity = activity;
    }

    public void showProgress() {
        mProgressDialog = PickerHelper.showProgress(mActivity, "加载主险参数");
    }

    public void cancelProgress() {
        if (mProgressDialog != null) {
            mProgressDialog.dismiss();
        }
    }


    /**
     * 点击主险
     *
     * @param listData
     * @param position
     */

    public void onItemClick(List<ResponseMain> listData, int position, ParamGetRisks param) {
        if (listData == null || listData.size() == 0) {
            submitMainChoose(null);
            return;
        }
        if (position < 0 || position > listData.size() - 1) {
            submitMainChoose(null);
            return;
        }
        ResponseMain data = listData.get(position);
        if (data == null) {
            submitMainChoose(null);
            return;
        }
        touDanInfo = new TouDanInfo(data.isOpenInsurance, data.insuranceUrl,data.productId);
        if (param != null) {
            getMainProductDetails(data.productId, DataUtils.formatRequestParamAge(param.age + "", param.birthday), param.sex, param.profession);
        } else {
            getMainProductDetails(data.productId, DataUtils.formatRequestParamAge("30", ""), 1, 1);
        }
    }

    /**
     * 提交主险选中
     *
     * @param list
     */
    public void submitMainChoose(List<InsuranceEntity> list) {
        MainChooseEvent event = new MainChooseEvent();
        event.list = list;
        event.touDanInfo = new TouDanInfo(touDanInfo);
        touDanInfo = null;
        EventBus.getDefault().post(event);
        ActivityFinishStack.getInstance().finishAllElement();
    }

    /**
     * 根据主险ID获取主险选项以及绑定附加险列表
     *
     */
    public void getMainProductDetails(int productId, String age, int sex, int profession) {
        showProgress();
        ProposalApi.getInstance(mActivity.getApplicationContext()).getMainDetails(productId, age, sex, profession, new OnResponseListener<ResponseMainDetail>() {
            @Override
            public void onSuccess(ResponseMainDetail body, ResponsePage page) {
                cancelProgress();
                if (body == null) {
                    submitMainChoose(null);
                    return;
                }
                body.parseOptions();
                submitMainChoose(DataUtils.toInsuranceList(body));
            }

            @Override
            public void onFailure(ResponseError error) {
                cancelProgress();
                submitMainChoose(null);
            }
        },(BaseIView) mActivity);
    }
}
